*! version 1.2
* 3 May 2012
* NIDS
* Coding employment status

*====================================================================================================================================
* NOTE		

/*
The following variables were used in the original do file. They are listed with their wave 1 names and their wave names (i.e. 
variable names/prefixes/suffixes across waves). They are converted to the wave names here.

w1			wave			w2
e1  	->	em1			->	e2
e18		->	em2			->	e23
e28		->	ems			->	e28
e40		->	emc			->	e48
e45		->	emp			->	e53
e52		->	emh			->	e61
e66		->	unemwnt		->	e74
e71_1a	->	unems* 		->	e79*
*/

set more off

version 12.0

*=====================================================================================================================================
* GLOBALS FOR DATA FILES, DO FILES AND VERSION SUFFIXES

global DataIN "\\137.158.104.21\data\Panel Public Release 2014a\Wave 2\Anon"
global DataOUT "C:\Users\01406074\Desktop"

global VersionIN "W2_Anon_V2.2"
global VersionOUT "Derived"

*=====================================================================================================================================

* ADULT

use "$DataIN\Adult_$VersionIN.dta", clear

cap renpfix "w2_a_" ""

gen done_nothing = 0
replace done_nothing = 1 if unems1 == 11

gen done_something = 0
forvalues x = 1/10 {
	forvalues t= 1/10 {
	replace done_something = 1 if unems`x' == `t'
	}
}

gen Employed= (em1==1|em2==1|ems==1|emc==1|emp==1|emh==1)
gen Not_economically_active= (Employed==0 & unemwnt==2)
gen Unemployed_discouraged= (Employed==0 & unemwnt==1 & done_nothing == 1)
gen Unemployed_strict= (Employed==0 & unemwnt==1 & done_something == 1)

gen state=.
replace state=0 if Not_economically_active==1
replace state=1 if Unemployed_discouraged==1
replace state=2 if Unemployed_strict==1
replace state=3 if Employed==1
replace state=-8 if state == . & outcome == 1

replace state=. if outcome != 1

cap la def state 0"Not Economically Active" 1"Unemployed_Discouraged" 2"Unemployed_Strict" 3"Employed" -8"Refused"
cap la val state state

cap ren state state_fine_a

gen state2=0 if state_fine_a == 0
replace state2=1 if state_fine_a >=1 & state_fine_a <=2
replace state2=2 if state_fine_a ==3
replace state2=-8 if state_fine_a == 4

cap la def state2 0"Not Economically Active" 1"Unemployed" 2"Employed" -8"Refused"
cap la val state2 state2
cap ren state2 state_coarse_a

keep pid w2_hhid state_fine_a  state_coarse_a outcome em1 em2 ems emc emh unemwnt unemmn done_nothing done_something unems* Employed Not_economically_active Unemployed_discouraged Unemployed_strict

sort  pid

tempfile 1_labour
save `1_labour', replace

*-------------------------------------------------------------------------------------------------------------------------------------

* PROXY

use "$DataIN\Proxy_$VersionIN.dta", clear

cap renpfix "w2_p_" ""

gen state2=.
 replace state2=0 if emp>=5 & emp<=8
 replace state2=2 if emp>=1 & emp<=4
 replace state2=1 if emp==9
 replace state2=-8 if outcome == 1 & state2 == .

cap la def state2 0"Not Economically Active" 1"Unemployed" 2"Employed" -8"Refused"
cap la val state2 state2
cap ren state2 state_coarse_p


keep pid w2_hhid state_coarse_p
sort pid

tempfile 2_labour
save `2_labour', replace

*-------------------------------------------------------------------------------------------------------------------------------------

* MERGING THE ABOVE INTO THE INDIVIDUAL DERIVED DATASET FOR RELEASE

use "$DataIN\indderived_$VersionIN.dta", clear

sort pid
merge 1:1 pid w2_hhid using "`1_labour'"
drop if _m == 2
drop _merge

sort pid
merge 1:1 pid w2_hhid using "`2_labour'"
drop if _m == 2
drop _merge


gen state_coarse_ap = .
replace state_coarse_ap = state_coarse_a if state_coarse_a !=.
replace state_coarse_ap = state_coarse_p if state_coarse_p !=.

cap la def state_coarse_ap 0"Not Economically Active" 1"Unemployed" 2"Employed" -8"Refused"
cap la val state_coarse_ap state_coarse_ap

cap ren state_coarse_ap w2_empl_stat_inclprox
cap ren state_fine_a w2_empl_stat

lab var w2_empl_stat_inclprox "Employment status - including Proxy"
lab var  w2_empl_stat "Employment status - Adult only"

cap drop outcome em1 ems em2 emc emh unemwnt unemmn unems1 unems2 unems3 unems4 unems5 unems6 unems7 unems8 unems9 unems10 unems11 unemsrcst unemsrsrc unemsrsrc_o
cap drop   Unemployed_strict Unemployed_discouraged Not_economically_active Employed done_something done_nothing
drop state_coarse_a state_coarse_p

save "$DataOUT\indderived_$VersionOUT.dta", replace

* end of do file
*=====================================================================================================================================
